package userHelp

import (
	myConfig "common/config"
	"common/helper"
	myredis "common/redis"
	"fmt"
	"time"
)

func GetDepositBonus(uid string) float64 {
	var sqlSelect string
	var disableRechargeShare float64 = 0.00

	sqlSelect = fmt.Sprintf("select disable_first_deposit_to_parent from tbl_member_return where uid=%s", uid)

	err := g_sql.Get(&disableRechargeShare, sqlSelect)
	if err != nil {
		helper.SqlErrLog("GetDepositBonus: sql:%s,reason:%s", sqlSelect, err.Error())
	}
	return disableRechargeShare
}

func FirstDepositToParent(uid string, ParentId string, amount, bonus float64, level int) {
	if myConfig.IsActiveOpen(myConfig.ActiveType_ProxyBonus_ByDepositAmount) == 0 {
		return
	}

	if ParentId != "0" && G_UserButton.GetCommiButton(ParentId) == ButtonClose {
		return
	}

	WagedReq := myConfig.GetGameCfgFloat64(myConfig.CfgFirstDepositBonusWaged)
	var validUser bool

	if GetTotalWaged(uid) >= WagedReq {
		validUser = true
	}

	//插入记录
	if validUser {
		sqlInsert := fmt.Sprintf("insert into tbl_rec_first_deposit_bonus(uid,nickname,parent_id,amount,level,rate,bonus,create_at,state)"+
			"values(%s,'%s',%s,%0.2f,%d,%0.3f,%0.3f,%d,1)", uid, GetMemberName(uid), ParentId, amount, level, 1.00, bonus, time.Now().Unix())
		myredis.AddLogToRedis(sqlInsert)
		AddEnableDepositBonus(ParentId, bonus)
		AutoDepositBonus(uid, ParentId, bonus)
	} else {
		sqlInsert := fmt.Sprintf("insert into tbl_rec_first_deposit_bonus(uid,nickname,parent_id,amount,level,rate,bonus,create_at,state)"+
			"values(%s,'%s',%s,%0.2f,%d,%0.3f,%0.3f,%d,0)", uid, GetMemberName(uid), ParentId, amount, level, 1.00, bonus, time.Now().Unix())
		myredis.AddLogToRedis(sqlInsert)
		AddDisableDepositBonus(uid, bonus)
	}
}

func AddEnableDepositBonus(uid string, bonus float64) {
	if bonus == 0.00 {
		return
	}
	var sqlUpdate string

	sqlUpdate = fmt.Sprintf("update tbl_member_return set enable_first_deposit_to_parent= enable_first_deposit_to_parent + %0.2f where uid=%s",
		bonus, uid)

	_, err := g_sql.Exec(sqlUpdate)
	if err != nil {
		helper.SqlErrLog("AddEnableDepositBonus: sql:%s,reason:%s", sqlUpdate, err.Error())
	}
}

func AddDisableDepositBonus(uid string, RechargeShare float64) {
	var sqlUpdate string

	sqlUpdate = fmt.Sprintf("update tbl_member_return set disable_first_deposit_to_parent=disable_first_deposit_to_parent+%0.2f where uid=%s",
		RechargeShare, uid)

	_, err := g_sql.Exec(sqlUpdate)
	if err != nil {
		helper.SqlErrLog("AddDisableDepositBonus: sql:%s,reason:%s", sqlUpdate, err.Error())
	}
}

func ClearEnableDepositBonus(uid string) {
	var sqlUpdate string
	sqlUpdate = fmt.Sprintf("update tbl_member_return set enable_first_deposit_to_parent=0.00 where uid=%s", uid)
	_, err := g_sql.Exec(sqlUpdate)
	if err != nil {
		helper.SqlErrLog("ClearEnableDepositBonus: sql:%s,reason:%s", sqlUpdate, err.Error())
	}
}

func ClearDisableDepositBonus(uid string) {
	var sqlUpdate string
	sqlUpdate = fmt.Sprintf("update tbl_member_return set disable_first_deposit_to_parent=0.00 where uid=%s", uid)
	_, err := g_sql.Exec(sqlUpdate)
	if err != nil {
		helper.SqlErrLog("ClearDisableDepositBonus: sql:%s,reason:%s", sqlUpdate, err.Error())
	}
}

/*
自动领取
*/
func AutoDepositBonus(uid string, parentId string, bonus float64) {

	if myConfig.IsActiveOpen(myConfig.ActiveType_ProxyBonus_ByDepositAmount) == 0 {
		sqlInsert := fmt.Sprintf("update tbl_rec_first_deposit_bonus set state=2 where uid=%s", uid)
		myredis.AddLogToRedis(sqlInsert)
		return
	}

	if bonus == 0.0 {
		return
	}

	CashType := helper.FirstDepositInviteBonus
	AddBalanceTransactionRecord(parentId, bonus, CashType, "0", helper.GenId(), 0.00, "", "", 0)
	AddUserWageItem(parentId, bonus, CashType)
	sqlInsert := fmt.Sprintf("update tbl_rec_first_deposit_bonus set state=1 where uid=%s and state=0", uid)
	myredis.AddLogToRedis(sqlInsert)
	AddUserBalanceByFloat64(parentId, bonus)
	ClearEnableDepositBonus(parentId)
	AddDonateAmountToParent(uid, bonus)
	AddTotalFirstDepositToParent(parentId, bonus)

	_, errExec := g_sql.Exec("CALL tbl_report_proxy_change_update(?,?,?,?)", parentId,
		CashType, bonus, time.Now().Unix())
	if errExec != nil {
		helper.GetloggerInstance().Emergencyf("CALL tbl_report_proxy_change_update(%s,%d,%0.2f,%d);	reason:%s",
			parentId, CashType, bonus, time.Now().Unix(), errExec.Error())
	}

	_, errExec = g_sql.Exec("CALL tbl_report_user_change_update(?,?,?,?)", parentId, CashType, bonus, time.Now().Unix())
	if errExec != nil {
		helper.GetloggerInstance().Emergencyf("CALL tbl_report_user_change_update(%s,%d,%0.2f,%d);	reason:%s",
			parentId, CashType, bonus, time.Now().Unix(), errExec.Error())
	}

	return
}
